package com.example.taobaounion.view.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.taobaounion.R;
import com.example.taobaounion.constant.Constants;
import com.example.taobaounion.model.domain.SelectContent;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SelectedPageContentAdapter extends RecyclerView.Adapter<SelectedPageContentAdapter.InnerHolder> {
    private List<SelectContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> mData = new ArrayList<>();
    private onSelectedPageContentItemClickListener mContentItemClickListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_page_content, parent, false);

        return new InnerHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {
        SelectContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean data = mData.get(position);

        holder.itemView.setOnClickListener(v -> {
            if (mContentItemClickListener != null) {
                mContentItemClickListener.onItemClick(data);
            }
        });

        holder.initView(data);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(SelectContent content) {
        if (content.getCode() == Constants.SUCCESS_CODE) {
            List<SelectContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean> uatm_tbk_item = content.getData().getTbk_uatm_favorites_item_get_response().getResults().getUatm_tbk_item();


            this.mData.clear();

            mData.addAll(uatm_tbk_item);

            notifyDataSetChanged();
        }
    }

    static class InnerHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.selected_cover)
        public ImageView cover;

        @BindView(R.id.select_page_off_price)
        public TextView offPrice;

        @BindView(R.id.select_page_original_price)
        public TextView originalPrice;

        @BindView(R.id.selected_buy_btn)
        public TextView buyBtn;

        @BindView(R.id.selected_title)
        public TextView title;

        InnerHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        void initView(SelectContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean data) {
            title.setText(data.getTitle());

            String pict_url = data.getPict_url();

            if (pict_url != null) {
                Glide.with(itemView.getContext()).load(pict_url).into(cover);
            }

            if (TextUtils.isEmpty(data.getCoupon_click_url()) || data.getZk_final_price() == null) {
                originalPrice.setText("该商品没有优惠劵");
                buyBtn.setVisibility(View.GONE);
            } else {
                originalPrice.setText(String.format("原价：%s元", data.getZk_final_price()));
            }


            if (TextUtils.isEmpty(data.getCoupon_info())) {
                offPrice.setVisibility(View.GONE);
            } else {
                offPrice.setVisibility(View.VISIBLE);
                offPrice.setText(data.getCoupon_info());
            }
        }
    }

    public void setOnSelectedPageContentItemClickListener(onSelectedPageContentItemClickListener listener) {
        this.mContentItemClickListener = listener;
    }

    public interface onSelectedPageContentItemClickListener {
        void onItemClick(SelectContent.DataBean.TbkUatmFavoritesItemGetResponseBean.ResultsBean.UatmTbkItemBean data);
    }
}
